const React = require('react');
const { useState } = React;
const Layout = require('@theme/Layout').default;
const styles = require('./feedback.module.css');

function Feedback() {
  const [formData, setFormData] = useState({
    name: '',
    email: '',
    type: 'Feature Request',
    content: '',
  });
  
  const [submitted, setSubmitted] = useState(false);
  const [error, setError] = useState('');
  const [isSubmitting, setIsSubmitting] = useState(false);

  const validateEmail = (email) => {
    const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return re.test(email);
  };

  const handleChange = (e) => {
    const { name, value } = e.target;
    setFormData(prevData => ({
      ...prevData,
      [name]: value
    }));
    setError('');
  };

  const handleSubmit = async (e) => {
    e.preventDefault();
    
    if (!formData.name.trim()) {
      setError('Please enter your name');
      return;
    }
    
    if (!formData.email.trim()) {
      setError('Please enter your email address');
      return;
    }

    if (!validateEmail(formData.email)) {
      setError('Please enter a valid email address');
      return;
    }
    
    if (!formData.content.trim()) {
      setError('Please enter your feedback');
      return;
    }

    setIsSubmitting(true);
    
    try {
      await new Promise(resolve => setTimeout(resolve, 1000));
      console.log('Form submitted:', formData);
      setSubmitted(true);
      setError('');
    } catch (err) {
      setError('Submission failed, please try again');
    } finally {
      setIsSubmitting(false);
    }
  };

  if (submitted) {
    return (
      <Layout title="Feedback Submitted - GitLink Help Center">
        <div className={styles.feedbackContainer}>
          <div className={styles.successMessage}>
            <h1>Thank you for your feedback!</h1>
            <p>We have received your feedback and will process it carefully.<br />Your support drives our continuous improvement.</p>
            <div>
              <button 
                className={styles.button}
                onClick={() => {
                  setSubmitted(false);
                  setFormData({
                    name: '',
                    email: '',
                    type: 'Feature Request',
                    content: '',
                  });
                }}
              >
                Submit Another Feedback
              </button>
              <a href="/" className={styles.homeLink}>Back to Home</a>
            </div>
          </div>
        </div>
      </Layout>
    );
  }

  return (
    <Layout title="Feedback - GitLink Help Center">
      <div className={styles.feedbackContainer}>
        <h1>Feedback</h1>
        <p className={styles.subtitle}>
          Your valuable feedback helps us improve
        </p>
        
        {error && <div className={styles.errorMessage}>{error}</div>}
        
        <form onSubmit={handleSubmit} className={styles.feedbackForm}>
          <div className={styles.formGroup}>
            <label htmlFor="name">Name <span className={styles.required}>*</span></label>
            <input
              type="text"
              id="name"
              name="name"
              value={formData.name}
              onChange={handleChange}
              placeholder="Please enter your name"
              disabled={isSubmitting}
              required
            />
          </div>
          
          <div className={styles.formGroup}>
            <label htmlFor="email">Email <span className={styles.required}>*</span></label>
            <input
              type="email"
              id="email"
              name="email"
              value={formData.email}
              onChange={handleChange}
              placeholder="Please enter your email address"
              disabled={isSubmitting}
              required
            />
          </div>
          
          <div className={styles.formGroup}>
            <label htmlFor="type">Feedback Type</label>
            <select
              id="type"
              name="type"
              value={formData.type}
              onChange={handleChange}
              disabled={isSubmitting}
            >
              <option value="Feature Request">Feature Request</option>
              <option value="Bug Report">Bug Report</option>
              <option value="Usage Inquiry">Usage Inquiry</option>
              <option value="Other">Other</option>
            </select>
          </div>
          
          <div className={styles.formGroup}>
            <label htmlFor="content">Feedback Content <span className={styles.required}>*</span></label>
            <textarea
              id="content"
              name="content"
              value={formData.content}
              onChange={handleChange}
              placeholder="Please describe your issue or suggestion in detail..."
              rows="6"
              disabled={isSubmitting}
              required
            />
          </div>
          
          <button 
            type="submit" 
            className={styles.button}
            disabled={isSubmitting}
          >
            {isSubmitting ? 'Submitting...' : 'Submit Feedback'}
          </button>
        </form>
      </div>
    </Layout>
  );
}

module.exports = Feedback; 